﻿using System.ComponentModel.DataAnnotations;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Runtime.Serialization;
using System;
using System.Xml.Linq;

namespace XamlaApp.ViewModels
{
    [DataContract]
    public sealed class TextBlockViewModel : FrameworkElementViewModel
    {
        public TextBlockViewModel()
        {
            Width = 100;
            Height = 30;
        }

        private string _text = "write something";
        [DataMember]
        public string Text
        {
            get { return _text; }
            set
            {
                _text = value;
                PropertyChanging("Text");
            }
        }

        private TextWrapping _wrapMode = TextWrapping.Wrap;
        [Display(Name = "Wrap Text?")]
        [DataMember]
        public TextWrapping Wrap
        {
            get
            {
                return _wrapMode;
            }
            set
            {
                _wrapMode = value;
                PropertyChanging("Wrap");
            }
        }

        private double _fontSize = 10;
        [Display(Name = "Font Size")]
        [DataMember]
        public double FontSize
        {
            get
            {
                return _fontSize;
            }
            set
            {
                _fontSize = value;
                PropertyChanging("FontSize");
            }
        }


        private TextAlignment _textAlignment = TextAlignment.Left;
        [Display(Name = "Text Alignment")]
        [DataMember]
        public TextAlignment Alignment
        {
            get
            {
                return _textAlignment;
            }
            set
            {
                _textAlignment = value;
                PropertyChanging("Alignment");
            }
        }

        private SolidColorBrush _foregroundBrush = new SolidColorBrush(Colors.Black);
        [IgnoreDataMember]
        public SolidColorBrush ForegroundBrush
        {
            get { return _foregroundBrush; }
            set
            {
                _foregroundBrush = value;
                PropertyChanging("ForegroundBrush");
            }
        }


        internal override System.Xml.Linq.XElement GenerateXaml(System.Xml.Linq.XElement element)
        {
            var el = new XElement("TextBlock",
                new XAttribute("Text",Text),
                new XAttribute("TextWrapping", Wrap),
                new XAttribute("FontSize", FontSize),
                new XAttribute("TextAlignment", Alignment),
                new XAttribute("Foreground", ForegroundBrush.Color.ToString()));

            //pass it to base view models for additional attributes
            var finishedXaml = base.GenerateXaml(el);

            //add it to the root xaml
            element.Add(finishedXaml);
            return element;
        }

        #region Json Proxy properties
        // These properties provide a serializable link to other view model properties that cannot be serialized inherently.
        // Sort of like value converters... these are not for databinding

        [DataMember]
        public Color ForegroundBrushProxy
        {
            get { return ForegroundBrush.Color; }
            set
            {
                if (_foregroundBrush == null) _foregroundBrush = new SolidColorBrush();
                ForegroundBrush.Color = value;
            }
        }
        #endregion
    }
}
